Mapping your domain classes via XML is pretty straightforward. Simply create a
hibernate.cfg.xml file in your project's
grails-app/conf/hibernate directory, either manually or via the
create-hibernate-cfg-xml command, that contains the following:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping resource="org.example.Book.hbm.xml"/>
…
</session-factory>
</hibernate-configuration>The individual mapping files, like 'org.example.Book.hbm.xml' in the above example, also go into the
grails-app/conf/hibernate directory. To find out how to map domain classes via XML, check out the
Hibernate manual.
If the default location of the
hibernate.cfg.xml file doesn't suit you, you can change it by specifying an alternative location in
grails-app/conf/DataSource.groovy:
hibernate {
config.location = "file:/path/to/my/hibernate.cfg.xml"
}or even a list of locations:
hibernate {
config.location = ["file:/path/to/one/hibernate.cfg.xml","file:/path/to/two/hibernate.cfg.xml"]
}Grails also allows you to write your domain model in Java or re-use an existing one that already has Hibernate mapping files. Simply place the mapping files into
grails-app/conf/hibernate and either put the Java files in
src/java or (if the domain model is stored in a JAR) the packaged classes into the project's
lib directory. You still need the
hibernate.cfg.xml though!